package lp.xian.materials.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import lp.xian.materials.domain.*;
import lp.xian.materials.mapper.*;
import lp.xian.materials.vo.QualityVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lp.xian.materials.service.IQualityService;

/**
 * 质检管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-15
 */
@Service
public class QualityServiceImpl implements IQualityService
{
    @Autowired
    private QualityMapper qualityMapper;
    @Autowired
    private WarehouseStockMapper warehouseStockMapper;
    @Autowired
    private WarehousingRequisitionTableMapper warehousingRequisitionTableMapper;
    @Autowired
    private StockMapper stockMapper;
    @Autowired
    private WarehouseDeptMapper warehouseDeptMapper;

    /**
     * 查询质检管理
     *
     * @param qualityId 质检管理主键
     * @return 质检管理
     */
    @Override
    public Quality selectQualityByQualityId(Integer qualityId)
    {
        return qualityMapper.selectQualityByQualityId(qualityId);
    }

    /**
     * 查询质检管理列表
     *
     * @param quality 质检管理
     * @return 质检管理
     */
    @Override
    public List<Quality> selectQualityList(Quality quality)
    {
        return qualityMapper.selectQualityList(quality);
    }

    /**
     * 新增质检管理
     *
     * @param quality 质检管理
     * @return 结果
     */
    @Override
    public int insertQuality(Quality quality)
    {
        quality.setCreateTime(DateUtils.getNowDate());
        return qualityMapper.insertQuality(quality);
    }

    /**
     * 修改质检管理
     *
     * @param quality 质检管理
     * @return 结果
     */
    @Override
    public int updateQuality(Quality quality)
    {
        quality.setUpdateTime(DateUtils.getNowDate());
        return qualityMapper.updateQuality(quality);
    }

    /**
     * 批量删除质检管理
     *
     * @param qualityIds 需要删除的质检管理主键
     * @return 结果
     */
    @Override
    public int deleteQualityByQualityIds(Integer[] qualityIds)
    {
        return qualityMapper.deleteQualityByQualityIds(qualityIds);
    }

    /**
     * 删除质检管理信息
     *
     * @param qualityId 质检管理主键
     * @return 结果
     */
    @Override
    public int deleteQualityByQualityId(Integer qualityId)
    {
        return qualityMapper.deleteQualityByQualityId(qualityId);
    }

    @Override
    public int auditQuality(QualityVo qualityVo) {
        List<QualityRaw> qualityRaws=qualityVo.getOrderList();
        Quality quality=new Quality();
        quality.setQualityStatus(1);
        quality.setQualityId(qualityVo.getQualityId());
        quality.setQualityPerson(SecurityUtils.getUsername());
        quality.setQualityDate(new Date());
        qualityMapper.updateQuality(quality);
        WarehouseStock warehouseStock=new WarehouseStock();
        warehouseStock.setWarehouseDeptId(qualityVo.getWarehouseDeptId());
        warehouseStock.setWarehouseStockOrder(qualityVo.getWarehousingRequisitionOrder());
        warehouseStock.setWarehouseStockType(0);
        warehouseStock.setWarehouseStockDate(new Date());
        warehouseStock.setCreateBy(SecurityUtils.getUsername());
        warehouseStock.setCreateTime(new Date());
        warehouseStockMapper.insertWarehouseStock(warehouseStock);
        for (int i = 0; i < qualityRaws.toArray().length; i++) {
            WarehousingRequisitionTable warehousingRequisitionTable=
                    warehousingRequisitionTableMapper.selectWarehousingRequisitionTableByWarehousingRequisitionTableId(qualityRaws.get(i).getWarehousingRequisitionTableId());
            Stock stock=new Stock();
            stock.setWarehouseStockId(warehouseStock.getWarehouseStockId());
            stock.setWarehouseStockPo(warehousingRequisitionTable.getWarehousingOrderNo());
            stock.setWarehouseStockNum(qualityRaws.get(i).getQualityNum());
            stock.setWarehouseStockName(qualityRaws.get(i).getQualityName());
            stock.setWarehouseStockSpeion(qualityRaws.get(i).getQualitySpeion());
            stock.setWarehouseStockUnit(warehousingRequisitionTable.getWarehousingRequisitionTableUnit());
            stock.setWarehouseStockPrice(warehousingRequisitionTable.getWarehousingRequisitionTablePrice());
            stock.setWarehouseStockTotalPrice(warehousingRequisitionTable.getWarehousingRequisitionTotalPrice());
            stock.setWarehouseStockTaxPrice(warehousingRequisitionTable.getWarehousingRequisitionTaxPrice());
            stock.setWarehouseStockUnitTotalPrice(warehousingRequisitionTable.getWarehousingRequisitionUnitTotalPrice());
            stockMapper.insertStock(stock);

        }
        //修改库位状态
        WarehouseDept warehouseDept=new WarehouseDept();
        warehouseDept.setWarehouseDeptId(qualityVo.getWarehouseDeptId());
        warehouseDept.setWarehouseDeptStatus(1);
        warehouseDeptMapper.updateWarehouseDept(warehouseDept);
        return 1;
    }
}
